﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Hizy.ViewModels;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace Hizy
{
    public partial class NewsViewer : PhoneApplicationPage
    {
        public NewsViewer()
        {
            InitializeComponent();
            DataContext = App.ViewModel;
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
            if (PhoneApplicationService.Current.State.ContainsKey("Item"))
            {
                LayoutRoot.DataContext = PhoneApplicationService.Current.State["Item"];
                App.AllDataModel.insertRecent((ItemViewModel)PhoneApplicationService.Current.State["Item"]);
            }
            RelatedBox.ItemsSource = App.AllDataModel.getRelated(((ItemViewModel)PhoneApplicationService.Current.State["Item"]).Id);
        }

        private void likeClick(object sender, EventArgs e)
        {
            if (PhoneApplicationService.Current.State.ContainsKey("Item"))
            {
                App.AllDataModel.insertBookmarks((ItemViewModel)PhoneApplicationService.Current.State["Item"]);
                MessageBox.Show("Article Saved!");
            }
        }
        private void RelatedChange(object sender, EventArgs e)
        {
            if (RelatedBox.SelectedItem == null)
                return;
            var guid = ((ItemViewModel)RelatedBox.SelectedItem).Id;

            // Navigate to the new page
            PhoneApplicationService.Current.State["Item"] = RelatedBox.SelectedItem;
            LayoutRoot.DataContext = RelatedBox.SelectedItem;
            scroll.ScrollToVerticalOffset(-400);
            RelatedBox.ItemsSource = App.AllDataModel.getRelated(((ItemViewModel)RelatedBox.SelectedItem).Id);
            //NavigationService.Navigate(new Uri("/NewsViewer.xaml", UriKind.Relative));

        }
        private void postClick(object sender, EventArgs e)
        {
            PhoneApplicationService.Current.State["Share"] = PhoneApplicationService.Current.State["Item"];
            NavigationService.Navigate(new Uri("/FacebookPage.xaml", UriKind.Relative));
        }
        private void tweetClick(object sender, EventArgs e)
        {
            PhoneApplicationService.Current.State["Share"] = PhoneApplicationService.Current.State["Item"];
            NavigationService.Navigate(new Uri("/TwitterPage.xaml", UriKind.Relative));
        }
        private void bookmarksClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Home.xaml?goto=1", UriKind.Relative));
        }
        private void searchClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/SearchResult.xaml", UriKind.Relative));

        }
        private void recentClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Home.xaml?goto=2", UriKind.Relative));
        }
        private void infoClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Home.xaml?goto=3", UriKind.Relative));

        }
        private void shareClickHandler(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/FacebookPage.xaml?global=yes", UriKind.Relative));
        }
        private void aboutClickHandler(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/About.xaml", UriKind.Relative));
        }
        private void appbar_StateChanged_1(object sender, ApplicationBarStateChangedEventArgs e)
        {

        }

        private void appbar_StateChanged_2(object sender, ApplicationBarStateChangedEventArgs e)
        {

        }
    }
}